class Cpanel::AdminsController < Cpanel::BaseController
  before_action :set_admin, only: [:show, :edit, :update, :destroy, :reset_password]

  def index
    respond_to do |format|
      format.html
      format.json { render json: AdminsDatatable.new(view_context, current_admin) }
    end
  end

  def show; end

  def new
    @admin = Admin.new
  end

  def edit; end

  def create
    @admin = Admin.new(admin_params)
    @admin.password = ENV['ADMIN_DEFAULT_PASSWORD']

    respond_to do |format|
      if @admin.save
        # add role to user
        admin_role_params.each do |role_name|
          @admin.add_role(role_name)
        end

        format.html { redirect_to cpanel_admin_path(@admin), notice: '保存成功' }
        format.json { render :show, status: :created, location: @admin }
      else
        format.html { render :new }
        format.json { render json: @admin.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @admin.update(admin_params)
        # remove role from user
        (AnAnCommon.resources.keys - admin_role_params).each do |role_name|
          @admin.remove_role(role_name)
        end

        # add role to user
        admin_role_params.each do |role_name|
          @admin.add_role(role_name)
        end

        # end
        format.html { redirect_to cpanel_admin_path(@admin), notice: '保存成功' }
        format.json { render :show, status: :ok, location: @admin }
      else
        format.html { render :edit }
        format.json { render json: @admin.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @admin.destroy
    render js: 'CPANEL.Admins.DataTable.refresh();CPANEL.alert_success("删除成功");'
  end

  def reset_password
    @admin.password = ENV['ADMIN_DEFAULT_PASSWORD']
    @admin.save
    render js: "CPANEL.alert_success(\"密码充值成功，密码为：#{ENV['ADMIN_DEFAULT_PASSWORD']}\");"
  end

  def edit_password
    @admin = current_admin
  end

  def update_password
    password_params = admin_password_params
    old_password = password_params[:old_password]

    @admin = current_admin
    if @admin.valid_password?(old_password)
      @admin.password = password_params[:password]
      @admin.password_confirmation = password_params[:password_confirmation]
    else
      @admin.errors.add(:base, '当前密码错误')
    end

    respond_to do |format|
      if @admin.valid_password?(old_password) && @admin.save
        format.html { redirect_to cpanel_root_path, notice: '密码修改成功' }
      else
        format.html { render :edit_password }
      end
    end
  end

  private

  def set_admin
    @admin = Admin.find(params[:id])
  end

  def admin_params
    params.require(:admin).permit(:email, :name)
  end

  def admin_role_params
    add_roles = params.require(:admin).permit(roles: []).try(:[], :roles) || []
    # 防止恶意添加垃圾权限
    AnAnCommon.resources.keys & add_roles
  end

  def admin_password_params
    params.require(:admin).permit(:old_password, :password, :password_confirmation)
  end
end
